#include <iostream>
#include "../Headers/log.h"

#include "tui.shell.h"

// Constructor
TOB::Shell::Shell(std::string title) {
  this->running = false;
  this->title = title;
}


// Destructor
TOB::Shell::~Shell() {
}


// Start listening
void TOB::Shell::start() {
  std::string line, cmd;

  this->running = true;
  while(this->running) {
    // Get line
    std::cout << std::endl << "> ";
    getline(std::cin, line);
    if(line.size() == 0) continue;

    // Parse
    std::stringstream stream(line);
    stream >> cmd;

    // Call function
    auto query = at.find(cmd);
    if(query != at.end()) {
      query->second(*this, stream);
    } else {
      error("Unknown function '" + cmd + "'");   
    }
  }
}


// Stop listening
void TOB::Shell::stop() {
  this->running = false;
}


// Ask text
TOB::Shell &TOB::Shell::getText(std::string &text, std::string msg) {
  std::cout << msg << ": ";
  getline(std::cin, text);
  return *this;
}


// Ask password
TOB::Shell &TOB::Shell::getPassword(std::string &password) {
  std::cout << "Password: ";
  getline(std::cin, password);
  return *this;
}


// Write string
TOB::Shell &TOB::Shell::write(std::string msg) {
  std::cout << msg;
  return *this;
}

// Write int
TOB::Shell &TOB::Shell::write(int32 msg) {
  std::cout << msg;
  return *this;
}

// Write unsigned int
TOB::Shell &TOB::Shell::write(uint32 msg) {
  std::cout << msg;
  return *this;
}

// Write line
TOB::Shell &TOB::Shell::writeLine() {
  std::cout << std::endl;
  return *this;
}


// Write error
TOB::Shell &TOB::Shell::error(std::string error) {
  std::cout << "ERROR: " << error << std::endl;
  Log::error(error);
  return *this;
}


// Wrong param
void TOB::Shell::wrongParam(std::string param) {
  error("'" + param + "' is wrong");
}
